﻿
namespace Data.Repository
{
	using System.Linq;
	using System.Collections.Generic;

    public class AddressRepository : Data.Repository.IAddressRepository
    {
        private LinqToECommerceDBDataContext _dbContext;

        #region IAddressRepository Members

        /// <summary>
        /// create a new address
        /// </summary>
        /// <param name="theNewAddress">the new address entity</param>
        /// <returns>the new address's id</returns>
		public int CreateNewAddress(Data.Entity.Address theNewAddress)
		{
			if(theNewAddress == null)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_Address_Create(theNewAddress.UserID, theNewAddress.IsBillingAddress, theNewAddress.FirstName, theNewAddress.LastName,
					theNewAddress.PhoneNumber, theNewAddress.Email, theNewAddress.FaxNumber, theNewAddress.Company, theNewAddress.Address1, theNewAddress.Address2,
					theNewAddress.CityID, theNewAddress.StateProvinceID, theNewAddress.ZipPostalCode, theNewAddress.CountryID, theNewAddress.CreatedOn, theNewAddress.UpdatedOn, theNewAddress.MobileNumber);
			}

			return result;
		}

		/// <summary>
		/// delete address
		/// </summary>
		/// <param name="id">the deleted address' id</param>
		/// <returns>0:deleted failed 1:deleted successfull</returns>
		public int DeleteAddress(int id)
		{
			if(id <= 0)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_Address_Delete(id);
			}

			return result;
		}

        public int UpdateAddress(Data.Entity.Address theAddress)
        {
            if (theAddress == null || theAddress.AddressId <= 0)
            {
                return 0;
            }

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var dbAddress = (from address in _dbContext.Addresses
								 where address.AddressId == theAddress.AddressId
								 select address).FirstOrDefault();

				if(dbAddress == null)
				{
					result = 0;
				}
				else
				{
					dbAddress.Address1 = theAddress.Address1;
					dbAddress.Address2 = theAddress.Address2;
					dbAddress.CityID = theAddress.CityID;
					dbAddress.Company = theAddress.Company;
					dbAddress.CountryID = theAddress.CountryID;
					dbAddress.Email = theAddress.Email;
					dbAddress.FaxNumber = theAddress.FaxNumber;
					dbAddress.FirstName = theAddress.FirstName;
					dbAddress.IsBillingAddress = theAddress.IsBillingAddress;
					dbAddress.LastName = theAddress.LastName;
					dbAddress.PhoneNumber = theAddress.PhoneNumber;
					dbAddress.StateProvinceID = theAddress.StateProvinceID;
					dbAddress.UpdatedOn = theAddress.UpdatedOn;
					dbAddress.UserID = theAddress.UserID;
					dbAddress.ZipPostalCode = theAddress.ZipPostalCode;
					dbAddress.CreatedOn = theAddress.CreatedOn;
					dbAddress.MobileNumber = theAddress.MobileNumber;

					_dbContext.SubmitChanges();

					result = 1;
				}
			}

			return result;
        }

		public IList<Data.Entity.Address> GetTheUserAddresses(System.Guid userID)
		{
			if(userID == null)
			{
				return null;
			}

			IList<ECommerce_GetTheUserAddressesResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetTheUserAddresses(userID);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.Address>();

				foreach(var item in lstData)
				{
					var theEntity = new Data.Entity.Address()
					{
						Address1 = item.Address1,
						Address2 = item.Address2,
						AddressId = item.AddressId,
						CityID = item.CityID,
						Company = item.Company,
						CountryID = item.CountryID,
						CreatedOn = item.CreatedOn,
						Email = item.Email,
						FaxNumber = item.FaxNumber,
						FirstName = item.FirstName,
						IsBillingAddress = item.IsBillingAddress,
						LastName = item.LastName,
						PhoneNumber = item.PhoneNumber,
						StateProvinceID = item.StateProvinceID,
						UpdatedOn = item.UpdatedOn,
						UserID = item.UserID,
						ZipPostalCode = item.ZipPostalCode,
						MobileNumber = item.MobileNumber
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		#endregion
	}
}
